/**
 * @Description: 
 * @Author: li kang
 * @Date: 2025-01-02 17:01:20
 * @LastEditTime: 2025-01-02 17:01:20
 * @LastEditors: li kang
 * @FilePath: \zt\h5\2024\20241228_kylx\js\openservice.js
*/
var isOnline = window.location.host.indexOf('jjl.cn') > -1;
var svcApi = ''
var targetTag = ''

if (isOnline) {
  svcApi = "https://svcapi-kefu.jjldxz.com";
} else {
  svcApi = "https://svcapi-kefu.jjldxz.net";
}

// // 唤起分公司指定国家客服
function openNationKefu(nation) {
  var tag = '',
  sysNo = window.companyId,
  tGid = '',
  cUid = '';
  if (nation) {
    nation = nation.trim()
    if (companySkillTags) {
      if(companySkillTags[nation]){
        tag = companySkillTags[nation].tag_number;
      }
      console.log(" companySkillTags[nation]", companySkillTags[nation]);
      if(!tag){
        // 取不到调综合客服
        sysNo = 7;
        tag = "";
        tGid = 7;
        cUid = "";
      }
      openCommonKf(sysNo, tag, tGid, cUid);
    } else {
      getZtKfCompany(function () {
        if(companySkillTags[nation]){
          tag = companySkillTags[nation].tag_number;
        }
        console.log(" companySkillTags[nation]", companySkillTags[nation]);
        if(!tag){
          // 取不到调综合客服
          sysNo = 7;
          tag = "";
          tGid = 7;
          cUid = "";
        }
        openCommonKf(sysNo, tag, tGid, cUid);
      });
    }
    
  } else {
    // 无国家参数调综合客服
    sysNo = 7;
    tag = "";
    tGid = 7;
    cUid = "";
    openCommonKf(sysNo, tag, tGid, cUid);
  }
}

function getZtKfCompany(callback) {
  $.ajax({
    url: svcApi + "/api/get_company_skillTags",
    type: "get",
    dataType: "json",
    contentType: "application/json",
    data: {
      company_id: window.companyId
    },
    success: function (res) {
      if (res.code == 0) {
        companySkillTags = res.data;
      }
      callback && callback();
      console.log("客服companySkillTags:", companySkillTags);
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      console.log("获取失败，请重试！CODE:" + XMLHttpRequest.status);
    },
  });
}

function openCommonKf(sysNo, tag, tGid, cUid) {
  var kf = kefuChat.getKefuChatInstance();
  kf.set("client", "consultWap");

  kf.set("grUserId", grUserId);

  var options = {
    sysNo: window.companyId ? window.companyId : 101, //分公司id（必传）
    tag: targetTag
  };
  var options = {
    sysNo: sysNo, //分公司id（必传）
    tag: tag, //客服标签id
    tGid: tGid, //客服技能组id（tag和tGid至少得传一个）
    cUid: cUid, //客服chatUid（指定客服必须传该参数）
  };
  console.log('打开客服参数', options)
  kf.openConsultEx(options);
}